package com.example.lt.service.video.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lt.entity.video.VideoStar;
import com.example.lt.mapper.video.VideoStarMapper;
import com.example.lt.service.video.VideoStarService;
import org.springframework.stereotype.Service;

/**
 * @Author 小涛Tao
 * @Date: 2024/08/09/21:19
 * version 1.0 注释：
 **/
@Service
public class VideoStarServiceImpl extends ServiceImpl<VideoStarMapper, VideoStar> implements VideoStarService {

    /**
     * 点赞
     * @param videoId 视频id
     * @param userId 用户id
     * @return true：点赞操作   false：取消点赞操作
     */
    @Override
    public boolean starVideo(Long videoId, Long userId) {
        final VideoStar videoStar = VideoStar.builder().userId(userId).videoId(videoId).build();
        try {
            // 有【userId】和【videoId】的组合唯一索引
            save(videoStar);
        } catch (Exception e) {
            remove(new LambdaQueryWrapper<VideoStar>().eq(VideoStar::getUserId, userId).eq(VideoStar::getVideoId, videoId));
            return false; // 取消点赞操作
        }
        return true; // 点赞操作
    }


    /**
     * 获取用户点赞状态
     * @param videoId 视频id
     * @param userId 用户id
     * @return
     */
    @Override
    public Boolean starState(Long videoId, Long userId) {
        if (userId == null) return false;
        return this.count(new LambdaQueryWrapper<VideoStar>().eq(VideoStar::getVideoId,videoId).eq(VideoStar::getUserId,userId)) == 1;
    }

}
